package be.webchess.www.service.impl;

import java.sql.SQLException;
import java.util.List;

import be.webchess.www.dao.GenericDAO;
import be.webchess.www.exceptions.ChessException;
import be.webchess.www.service.IGenericService;

public class GenericService<obj> implements IGenericService<obj> {
	protected GenericDAO<obj> 	dao;
	
	@Override
	public obj findByID(Long id) throws ChessException {

		try {
			obj o = dao.findByID(id);
			//dao.commit();
			return o;
		} catch ( SQLException ex )
		{
			try { 
				dao.rollback();			
			}catch ( SQLException ex2 )
			{				
			}
			
			throw new ChessException(ex);
		}
	}

	@Override
	public obj insert(obj o) throws ChessException {
		
		try {
			obj a = dao.insert(o);
			//dao.commit();
			return a;			
		} catch ( SQLException ex )
		{
			try { 
				dao.rollback();			
			}catch ( SQLException ex2 )
			{				
			}
			
			throw new ChessException(ex);			
		}		
	}

	@Override
	public obj update(obj o) throws ChessException {

		try {
			obj a = dao.update(o);
			//dao.commit();
			return a;			
		} catch ( SQLException ex )
		{
			try { 
				dao.rollback();			
			}catch ( SQLException ex2 )
			{				
			}
			
			throw new ChessException(ex);			
		}
	}

	@Override
	public void delete(obj o) throws ChessException {
		
		try {
			dao.delete(o);
			//dao.commit();
		} catch ( SQLException ex )
		{
			try { 
				dao.rollback();			
			}catch ( SQLException ex2 )
			{				
			}
			
			throw new ChessException(ex);			
		}
	}

	@Override
	public List<obj> findAll() throws ChessException {

		try {
			List<obj> o = dao.findAll();
			//dao.commit();
			return o;			
		} catch ( SQLException ex )
		{
			try { 
				dao.rollback();			
			}catch ( SQLException ex2 )
			{				
			}
			
			throw new ChessException(ex);			
		}		
	}

}
